package com.tx.order.controller;

import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.tx.order.pojo.Order;
import com.tx.order.pojo.OrderInfo;
import com.tx.order.service.OrderService;
import com.tx.user.pojo.User;

@Controller
public class OrderController {

	@Autowired
	private OrderService service;
	
	@RequestMapping("/selectAllOrder.do")
	@ResponseBody
	public List<Order> selectAllOrder(){
		List<Order> list = service.selectAllOrder();
		return list;
	}
	
	@RequestMapping("/addOrder.do")
	public String addOrder(Order order, String[] did, String[] remark, HttpSession session) {
		User u = (User) session.getAttribute("user");
		order.setUid(u.getUid());
		order.setOaddress("1309");
		order.setCount(did.length+"");
		boolean result = service.addOrder(order);
		System.out.println("添加订单的结果为：" + result);
		String oid = order.getOid();
		if(did != null) {
			for(int i = 0; i < did.length; i++) {
				OrderInfo orderinfo = new OrderInfo(oid, did[i], remark[i]);
				service.addOrderInfo(orderinfo);
			}
		}
		return "orderList";
	}
}
